package model

import (
	"time"
)

type User struct {
	UserId int64 `xorm:"pk autoincr"`
	UserName string
	RoleId int64
	CreatedAt time.Time
  	UpdatedAt time.Time
}

func (u *User)QueryUserList(cnId int64) []*User{
	var uvList []*User
	x.Join("INNER", "check_node_user", "check_node_user.user_id = user.user_id" ).Where("check_node_user.check_node_id = ?", cnId).Find(&uvList)
	return uvList
}

func (u *User)QueryUserCheckNodeList(uId int64) []*CheckNode{
	var checkNodeList []*CheckNode
	x.Join("INNER", "check_node_user", "check_node_user.check_node_id = check_node.check_node_id" ).Where("check_node_user.user_id = ?", uId).Find(&checkNodeList)
	return checkNodeList
}

func (u *User)DeleteUser(uId int64) (map[string]interface{}, error){
	var p *Paper

	for _, userCheckNode := range u.QueryUserCheckNodeList(uId) {
		if len(p.QueryPaperList(userCheckNode.CheckNodeId)) > 0 && len(u.QueryUserList(userCheckNode.CheckNodeId)) == 1 {
			return map[string]interface{}{"message": "该节点下还存在未审单且该节点下只有此审单员一人", "code": "400"}, nil
		}
	}

	var user User
	x.Id(uId).Delete(&user)
	return map[string]interface{}{"message": "成功","code":"200"}, nil
}